unit frmCadModelo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, DB, MDOCustomDataSet, MDOQuery, jpeg, ExtCtrls,
  StdCtrls, Buttons, Grids, DBGrids, XPMan;

type
  ThackGrid = class (TDBGrid);
  TF_ = class(TForm)
    Panel1: TPanel;
    imgTop: TImage;
    Ds: TDataSource;
    pgDados: TPageControl;
    tsCons: TTabSheet;
    tsDados: TTabSheet;
    pngBotton: TPanel;
    btnOk: TBitBtn;
    btnNovo: TBitBtn;
    btnExcluir: TBitBtn;
    btnCons: TBitBtn;
    pnConsulta: TPanel;
    dbCons: TDBGrid;
    Panel2: TPanel;
    lbDes: TLabel;
    EditCons: TEdit;
    Panel3: TPanel;
    lbTitulo1: TLabel;
    lbTitulo: TLabel;
    QryCons: TMDOQuery;
    QryDados: TMDOQuery;
    QryGeral: TMDOQuery;
    btnSair: TBitBtn;

    procedure dbConsDblClick(Sender: TObject);
    procedure btnConsClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dbConsDrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
    procedure FormCreate(Sender: TObject);
    procedure btnNovoClick(Sender: TObject);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure btnSairClick(Sender: TObject);
    procedure btnOkClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure dbConsTitleClick(Column: TColumn);

  private
     procedure eventosMessage(var Msg: tagMSG;  var Handled: Boolean);

    { Private declarations }
  public
    { Public declarations }
    Procedure Inicio(sTabNome :String);
  end;

var
  F_: TF_;
  sSis, sNomeTab :String;
  Ascendente :Boolean;
  iAnula     :Integer;

implementation

uses frmDMGeral, frmFunctions;

{$R *.dfm}

Procedure TF_.Inicio(sTabNome :String);
begin
  show;
  sSis := 'AL Sistemas';
  lbTitulo.Caption := sSis;
  lbTitulo1.Caption := lbTitulo.Caption;
  sNomeTab := sTabNome;
end;

procedure TF_.dbConsDblClick(Sender: TObject);
begin
  pgDados.ActivePage := tsDados;
end;

procedure TF_.btnConsClick(Sender: TObject);
begin
  pgDados.ActivePage := tsCons;
end;

procedure TF_.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action := caFree;
  //F_ := nil;
end;

procedure TF_.dbConsDrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  s :String;
begin
  try
    with dbCons do begin
       if Odd( DataSource.DataSet.RecNo) then
         Canvas.Brush.Color := $00E0E0E0   //cor 2
       else
         Canvas.Brush.Color := clWindow; //cor 1
        Canvas.FillRect(Rect);
       DefaultDrawColumnCell(Rect,DataCol,Column,State);
     end;
  except
  end;

 if (ThackGrid(dbCons).DataLink.ActiveRecord + 1 = ThackGrid(dbCons).Row)
    or (gdFocused in State) or (gdSelected in State) then begin
     dbCons.canvas.Brush.Color := $00FDF3EC;
     dbCons.canvas.Font.Color := clGrayText;
 end;
  dbCons.DefaultDrawColumnCell (Rect, DataCol, Column, State);
  try
    if Column.Field.DataType = ftblob then begin
      s := trim(Copy(Column.Field.AsString,1,30));
      dbCons.Canvas.TextRect(Rect,Rect.Left,Rect.Top,s);
    end;
  except
  end;
end;

procedure TF_.FormCreate(Sender: TObject);
begin

//  QryCons.Open;
  Application.OnMessage := eventosMessage;
end;

procedure TF_.eventosMessage(var Msg: tagMSG;  var Handled: Boolean);
var
   i: SmallInt;
begin
   if Msg.message = WM_MOUSEWHEEL then
   begin
     Msg.message := WM_KEYDOWN;
     Msg.lParam := 0;
     i := HiWord(Msg.wParam);
     if i > 0 then
       Msg.wParam := VK_UP
     else
       Msg.wParam := VK_DOWN;

     Handled := False;
   end;
end;


procedure TF_.btnNovoClick(Sender: TObject);
begin
  LimparCampos(Self);
end;



// Tab to Enter
procedure TF_.FormKeyPress(Sender: TObject; var Key: Char);
var
  x              : integer;
  iTabOrder      : integer;
  controlAtual   : TComponent;
  sTituloOriginal: string;
begin
  if (Key = Char(VK_ESCAPE)) then
    btnSairClick(btnSair)
  else if (Key = Char(VK_RETURN)) then begin
    controlAtual := TForm(self).ActiveControl;
    if (controlAtual is TEdit) then
      iTabOrder := TEdit(controlAtual).TabOrder
    else if (controlAtual is TDateTimePicker) then
      iTabOrder := TDateTimePicker(controlAtual).TabOrder
    else if (controlAtual is TRadioButton) then
      iTabOrder := TRadioButton(controlAtual).TabOrder;
    for x := 0 to (self.ComponentCount) - 1 do begin
      if (self.Components[x] is TCustomEdit) then begin // Custom edit
        if ((self.Components[x] as TCustomEdit).TabOrder = (iTabOrder + 1)) then
        begin
          (self.Components[x] as TCustomEdit).SetFocus;
          exit;
        end;
      end
      else if (self.Components[x] is TEdit) then begin // TextEdit
        if ((self.Components[x] as TEdit).TabOrder = (iTabOrder + 1)) then begin
          (self.Components[x] as TEdit).SetFocus;
          exit;
        end;
      end
      else if (self.Components[x] is TComboBox) then begin // Combobox
        if ((self.Components[x] as TComboBox).TabOrder = (iTabOrder + 1)) then
        begin
          (self.Components[x] as TComboBox).SetFocus;
          exit;
        end;
      end
      else if (self.Components[x] is TCheckBox) then begin // CheckBox
        if ((self.Components[x] as TCheckBox).TabOrder = (iTabOrder + 1)) then
        begin
          (self.Components[x] as TCheckBox).SetFocus;
          exit;
        end;
      end
      else if (self.Components[x] is TRadioButton) then begin // CheckBox
        if ((self.Components[x] as TRadioButton).TabOrder = (iTabOrder + 1))
        then begin
          (self.Components[x] as TRadioButton).SetFocus;
          exit;
        end;
      end
      else if (self.Components[x] is TDateTimePicker) then begin
        // Date Time Picker
        if ((self.Components[x] as TDateTimePicker).TabOrder = (iTabOrder + 1))
        then begin
          (self.Components[x] as TDateTimePicker).SetFocus;
          exit;
        end;
      end;
    end;
    btnOkClick(btnOk);
    //
    // Key := #0;
    // Perform(WM_NEXTDLGCTL, 0, 0);
  end;
end;

procedure TF_.btnSairClick(Sender: TObject);
begin
  Close;
end;

procedure TF_.btnOkClick(Sender: TObject);
begin
 //
end;

procedure TF_.FormShow(Sender: TObject);
begin
  Ascendente := True;
end;

procedure TF_.dbConsTitleClick(Column: TColumn);
begin
  if iAnula = 1 then
    abort;
  if  Ascendente then begin
    buscar(QryCons,sNomeTab,'','',Column.FieldName+' ASC ',4);
    Ascendente := False;
  end else begin
    buscar(QryCons,sNomeTab,'','',Column.FieldName+' DESC ',4);
    Ascendente := True;
  end;
end;
end.
